<template>
  <view class="container">
    <uni-nav-bar
      :border="false"
      statusBar
      title="详情"
      color="#000"
      left-icon="left"
      backgroundColor="transparent"
      @clickLeft="onBack"
    >
    </uni-nav-bar>

    <view class="box">
      <view class="mainBox">
        <view class="id"
          >{{ $util.selectDictLabel(appStore.areaData, data.lockerLocationId)
          }}{{
            data.lockerGroupId
              ? `(${$util.selectDictLabel(
                  appStore.groupData,
                  data.lockerGroupId
                )})`
              : ''
          }}
          - <text>{{ data?.number }}</text></view
        >
        <!-- <view class="val">{{ $util.selectDictLabel(allocateData, data?.status) }}</view> -->
        <view class="status"
          >状况{{ $util.selectDictLabel(lockerStatus, data?.conditions) }}</view
        >
      </view>
      <view class="infoBox">
        <view class="item">
          <text class="name">状态</text>
          <text class="val">{{
            $util.selectDictLabel(allocateData, data?.status)
          }}</text>
        </view>
        <view class="item">
          <text class="name">使用人</text>
          <text class="val">{{
            data.personName ||
            $util.selectDictLabel(appStore.personData, data.personId)
          }}</text>
        </view>
        <view class="item">
          <text class="name">所在部门</text>
          <text class="val">{{
            $util.selectDictLabel(appStore.deptData, data.deptId)
          }}</text>
        </view>
        <view class="item">
          <text class="name">职级</text>
          <text class="val">{{ data?.rank }}</text>
        </view>
        <view class="item">
          <text class="name">经办人</text>
          <text class="val">{{
            $util.selectDictLabel(appStore.personData, data.claimingPersonId)
          }}</text>
        </view>
        <view class="item">
          <text class="name">分配时间</text>
          <text class="val">{{ data?.claimingTime }}</text>
        </view>
      </view>
      <view class="btnBox">
        <button class="btn" v-if="data?.status == 0" @click="onUpdate(0)">
          分配
        </button>
        <button class="btn" v-if="data?.status == 1" @click="onUpdate(1)">
          退还
        </button>
      </view>
    </view>
    <view class="footer">
      <button class="btn" type="primary" @click="onEdit">编辑</button>
      <button class="btn" type="warn" @click="onDelete">删除</button>
    </view>

    <uni-popup ref="popup">
      <FormView :data="data" :type="type" @callback="onCallback" />
    </uni-popup>
  </view>
</template>

<script>
import {
  delLocker,
  getLockerDetail,
  netLockerReturn,
  netLockerUpdate,
} from '@/api/locker';
import FormView from './form.vue';
import useAppStore from '@/store/modules/app';
export default {
  components: {
    FormView,
  },
  data() {
    return {
      lockerStatus: [
        {
          label: '良好',
          text: '良好',
          value: 0,
        },
        {
          label: '异常',
          text: '异常',
          value: 1,
        },
        {
          label: '维修中',
          text: '维修中',
          value: 2,
        },
      ],
      allocateData: [
        {
          label: '未分配',
          text: '未分配',
          value: 0,
        },
        {
          label: '已分配',
          text: '已分配',
          value: 1,
        },
      ],
      data: {},
      query: {},
      // 分配退还的类型，因为表单不一样
      type: 0,
      // store
      appStore: useAppStore(),
    };
  },
  onLoad(query) {
    this.query = query;
    this.getData();
  },
  methods: {
    getData() {
      getLockerDetail(this.query.bathroomLockerId).then((res) => {
        this.data = res.data;
      });
    },
    onEdit() {
      // url参数
      let obj = {
        bathroomLockerId: this.query.bathroomLockerId,
      };
      let str = this.$util.jsonToUrlParam(obj);
      // 跳转
      this.onLocation(`/pages/locker/edit?${str}`);
    },
    // 删除
    onDelete() {
      uni.showModal({
        title: '提示',
        content: '是否确认删除？',
        success: (res) => {
          if (res.confirm) {
            delLocker(this.query.bathroomLockerId).then((res) => {
              uni.showToast({
                title: `删除成功`,
              });
              this.onBack();
            });
          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        },
      });
    },
    // 更新
    onUpdate(t) {
      this.type = t;
      this.$refs.popup.open('bottom');
    },
    onCallback(r) {
      // loading提示
      uni.showLoading({ title: '提交中' });
      console.log(r);
      let params = {
        ...this.data,
        ...r,
      };
      let api = this.type === 0 ? netLockerUpdate : netLockerReturn;
      api(params).then((res) => {
        uni.hideLoading();
        uni.showToast({
          title: `操作成功`,
        });
        // 刷新详情
        this.getData();
        this.$refs.popup.close();
      });
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - 160rpx);
  padding-bottom: calc(
    var(--window-bottom) + constant(safe-area-inset-bottom) + 160rpx
  );
  padding-bottom: calc(
    var(--window-bottom) + env(safe-area-inset-bottom) + 160rpx
  );
  background: linear-gradient(180deg, #d7e4fd 0%, #fff 50%);
  background-repeat: no-repeat;
  background-size: 100% auto;
  position: relative;
  .box {
    margin: 50rpx 36rpx 0;
    padding: 24rpx;
    background: #ffffff;
    border-radius: 12rpx;
    box-shadow: 0 0 30rpx 0 #ccc;
    .mainBox {
      text-align: center;
      padding: 40rpx 0;
      line-height: 1;

      .id {
        font-size: 38rpx;
        font-weight: bold;
        color: #4288ff;
        padding-bottom: 40rpx;
      }

      .val {
        font-size: 40rpx;
        font-weight: 800;
        color: #ff884d;
        padding-bottom: 27rpx;

        text {
          font-size: 30rpx;
          margin-left: 10rpx;
        }
      }

      .status {
        font-size: 26rpx;
        font-weight: 500;
        color: #555555;
      }
    }
    .infoBox {
      .item {
        font-size: 30rpx;
        font-weight: 500;
        padding: 22rpx 12rpx;
        display: flex;
        justify-content: space-between;
        border-bottom: 2rpx solid #eaeaea;
        &:last-child {
          border-bottom: 0;
        }
        .name {
          color: #989898;
        }

        .val {
          width: 60%;
          color: #333;
          text-align: right;
          word-break: break-all;
        }
      }
    }
  }
  .myLabel {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .star {
      color: #dd524d;
    }
  }
  .footer {
    display: flex;
    justify-content: space-evenly;
    position: fixed;
    left: 0;
    right: 0;
    bottom: calc(
      var(--window-bottom) + constant(safe-area-inset-bottom) + 60rpx
    );
    bottom: calc(var(--window-bottom) + env(safe-area-inset-bottom) + 60rpx);
    .btn {
      width: 40%;
      margin: 0;
    }
  }
  .btnBox {
    margin: 60rpx 0 10rpx;
    .btn {
      background: linear-gradient(
          to bottom,
          #d5dee7 0%,
          #e8ebf2 50%,
          #e2e7ed 100%
        ),
        linear-gradient(
          to bottom,
          rgba(0, 0, 0, 0.02) 50%,
          rgba(255, 255, 255, 0.02) 61%,
          rgba(0, 0, 0, 0.02) 73%
        ),
        linear-gradient(
          33deg,
          rgba(255, 255, 255, 0.2) 0%,
          rgba(0, 0, 0, 0.2) 100%
        );
      background-blend-mode: normal, color-burn;
    }
  }
}
</style>
